package cn.schoolwow.util.domain.query.jsonarray;

import cn.schoolwow.util.module.query.jsonarray.domain.JSONArrayQueryOption;
import com.alibaba.fastjson.JSONArray;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class QueryJSONArray {
    private JSONArrayQueryOption jsonQueryOption = new JSONArrayQueryOption();

    /**创建实例列表查询*/
    public static <T> QueryJSONArray newQuery(JSONArray array){
        QueryJSONArray jsonQuery = new QueryJSONArray();
        jsonQuery.jsonQueryOption.array = array;
        return jsonQuery;
    }

    /**是否去重*/
    public QueryJSONArray distinct(){
        jsonQueryOption.distinct = true;
        return this;
    }

    /**添加字段值查询*/
    public QueryJSONArray addQuery(String fieldName, Object value){
        jsonQueryOption.fieldValueMap.put(Arrays.asList(fieldName), value);
        return this;
    }

    /**添加字段列表查询*/
    public QueryJSONArray addInQuery(String fieldName, List<Object> valueList){
        jsonQueryOption.fieldValueInListMap.put(Arrays.asList(fieldName), valueList);
        return this;
    }

    /**添加字段列表查询*/
    public QueryJSONArray addNotInQuery(String fieldName, Collection valueCollection){
        jsonQueryOption.fieldValueNotInListMap.put(Arrays.asList(fieldName), valueCollection);
        return this;
    }

    /**添加字段列表查询*/
    public QueryJSONArray addInQuery(Collection<String> fieldNames, Collection valueCollection){
        if(null!=valueCollection&&!valueCollection.isEmpty()){
            jsonQueryOption.fieldValueInListMap.put(fieldNames, valueCollection);
        }
        return this;
    }

    /**添加字段不在列表查询*/
    public QueryJSONArray addNotInQuery(Collection<String> fieldNames, Collection valueCollection){
        if(null!=valueCollection&&!valueCollection.isEmpty()){
            jsonQueryOption.fieldValueNotInListMap.put(fieldNames, valueCollection);
        }
        return this;
    }

    /**添加字段值查询*/
    public QueryJSONArray addQuery(Collection<String> fieldNames, String value){
        jsonQueryOption.fieldValueMap.put(fieldNames, value);
        return this;
    }

    /**设置下划线数据*/
    public QueryJSONArray underline(){
        jsonQueryOption.underline = true;
        return this;
    }

    /**指定升序排序字段*/
    public QueryJSONArray orderByAsc(String fieldName){
        jsonQueryOption.orderByAscFieldName = fieldName;
        return this;
    }

    /**指定降序排序字段*/
    public QueryJSONArray orderByDesc(String fieldName){
        jsonQueryOption.orderByDescFieldName = fieldName;
        return this;
    }

    public QueryJSONArrayResponse execute(){
        return new QueryJSONArrayResponse(jsonQueryOption);
    }
}
